/**
 * Created with IntelliJ IDEA.
 * User: liulinhui
 * Date: 2019/12/9
 * Time: 1:30 PM
 * Description: index.js
 */
const {should, expect, assert} = require('chai');
const request = require('superagent');
const baseUrl = 'http://localhost:8001/app';
const headers = {
    _v: '0.1',
    AUTH_TOKEN: 'eyJhbGciOiJSUzI1NiJ9.eyJ0IjoxNTkxMDIwMTA1NTIxLCJiIjoidV8xMjY3NDU2MzAyMTk3NTE0MjQwIiwidiI6MTI2NzQ1NjMwMjE5NzUxNDI0MX0.I_F8uXlBWKbFZAEpny8MB0jPIfcn2pe-dW1Gtyhl6gc8xW6zpFAGMS1T5PBV_X1jm32ahKUCjZk_LzPPnraOjtgFXM_PhbXAMnlQpjEKEPwDGPo4Nacul4G-ipXeRA-O57Jn9ZnbejagyUEqhDrPsHk_NSHB5B24fCyccZxpSTc',
    content_type: 'application/json;charset=UTF-8',
};

function callback(err, res, done) {
    console.info(JSON.stringify(res.body, null, 4));
    assert.equal(res.body.code, 200);
    done()
}

exports.POST = function ({url, token, login = true, _v, data, doneCall}) {
    request
        .post(baseUrl + url)
        .send(data)
        .set('_v', _v || headers._v)
        .set('AUTH-TOKEN', login ? token || headers.AUTH_TOKEN : '')
        .set('content-type', headers.content_type)
        .end(function (err, res) {
            return callback(err, res, doneCall)
        })
};

exports.GET = function ({url, token, login = true, _v, data, doneCall}) {
    request
        .get(baseUrl + url)
        .query(data)
        .set('openId', _v || headers._v)
        .set('AUTH-TOKEN', login ? token || headers.AUTH_TOKEN : '')
        .set('content-type', headers.content_type)
        .end(function (err, res) {
            return callback(err, res, doneCall)
        })
};
